#include "ImageMgr.h"

#include <QDir>
#include <QGuiApplication>

ImgMgr& ImgMgr::instance() {
  static ImgMgr im;
  return im;
}

void ImgMgr::registerImgs(QString module) {
  QString rootPath(":/");
  rootPath.append(module).append("/image");

  QDir root(rootPath);
  QFileInfo imgDirFi;
  foreach (imgDirFi, root.entryInfoList()) {
    if (!imgDirFi.isDir()) continue;

    QDir imgDir(imgDirFi.filePath());
    QFileInfo imgFi;

    QString imgName = module + "_" + imgDirFi.fileName();
    m_imgs[imgName].resize(8);
    foreach (imgFi, imgDir.entryInfoList()) {
      if (imgFi.isDir()) continue;

      // TODO: check locale
      QString imgFileName = imgFi.baseName();
      QString imgPath = imgFi.absoluteFilePath();
      if (imgFileName.endsWith("normal"))
        m_imgs[imgName][0] = new SImage(imgPath);
      else if (imgFileName.endsWith("normal@2")) {
        m_imgs[imgName][4] = new SImage(imgPath);
        m_imgs[imgName][4]->setDevicePixelRatio(2);
      } else if (imgFileName.endsWith("hover"))
        m_imgs[imgName][1] = new SImage(imgPath);
      else if (imgFileName.endsWith("hover@2")) {
        m_imgs[imgName][5] = new SImage(imgPath);
        m_imgs[imgName][5]->setDevicePixelRatio(2);
      } else if (imgFileName.endsWith("pressed"))
        m_imgs[imgName][2] = new SImage(imgPath);
      else if (imgFileName.endsWith("pressed@2")) {
        m_imgs[imgName][6] = new SImage(imgPath);
        m_imgs[imgName][6]->setDevicePixelRatio(2);
      } else if (imgFileName.endsWith("disabled"))
        m_imgs[imgName][3] = new SImage(imgPath);
      else if (imgFileName.endsWith("disabled@2")) {
        m_imgs[imgName][7] = new SImage(imgPath);
        m_imgs[imgName][7]->setDevicePixelRatio(2);
      }
    }
  }
}

SImage* ImgMgr::image(QString imgId, WidgetStatus type, float scale) {
  if (m_imgs.constFind(imgId) == m_imgs.end()) return nullptr;
  if (scale < 0) scale = qApp->devicePixelRatio();

  int index = static_cast<int>(type);
  if (scale - 1.5 > -1E-5) {
    index = index + 4;
    if (!m_imgs[imgId][index]) index = index - 4;
  }
  return m_imgs[imgId][index];
}

QString ImgMgr::imagePath(QString imgId, WidgetStatus type, float scale) {
  SImage* img = ImgMgr::image(imgId, type, scale);
  if (img) {
    return img->imgPath();
  }
  return "";
}

void ImgMgr::clean() {
  for (auto imgs : m_imgs) {
    for (auto img : imgs) {
      delete img;
    }
    imgs.clear();
  }
  m_imgs.clear();
}

SImage::SImage(const QString& path) {
  m_img = new QImage(path);
  m_imgPath = path;
}

SImage::~SImage() { delete m_img; }

void SImage::setDevicePixelRatio(float f) { m_img->setDevicePixelRatio(f); }

float SImage::devicePixelRatio() const { return m_img->devicePixelRatio(); }

QSize SImage::size() const { return m_img->size() / m_img->devicePixelRatio(); }

QRect SImage::rect() const { return m_img->rect(); }

SImage::operator QImage() { return *m_img; }

float SImage::width() const {
  return m_img->width() / m_img->devicePixelRatio();
}

float SImage::height() const {
  return m_img->height() / m_img->devicePixelRatio();
}
